package cn.com.victorysoft.ylgl.service.commons.web;

import org.apache.commons.lang3.builder.ToStringBuilder;

/***
 * @author gechaoqing
 */
public class JsonMessage {
    private static final String OK = "ok";
    private static final String ERROR = "error";
    private JsonMessage.Meta meta;
    private Object data;

    public JsonMessage() {
    }

    public JsonMessage success() {
        this.meta = new JsonMessage.Meta(true, "ok");
        return this;
    }

    public JsonMessage success(Object data) {
        this.meta = new JsonMessage.Meta(true, "ok");
        this.data = data;
        return this;
    }

    public JsonMessage failure() {
        this.meta = new JsonMessage.Meta(false, "error");
        return this;
    }

    public JsonMessage failure(String message) {
        this.meta = new JsonMessage.Meta(false, message);
        return this;
    }

    public JsonMessage.Meta getMeta() {
        return this.meta;
    }

    public Object getData() {
        return this.data;
    }

    @Override
    public String toString() {
        return (new ToStringBuilder(this)).append("meta", this.meta).append("data", this.data).toString();
    }

    public class Meta {
        private boolean success;
        private String message;

        public Meta(boolean success, String message) {
            this.success = success;
            this.message = message;
        }

        public boolean isSuccess() {
            return this.success;
        }

        public String getMessage() {
            return this.message;
        }

        @Override
        public String toString() {
            return (new ToStringBuilder(this)).append("success", this.success).append("message", this.message).toString();
        }
    }
}
